package per.kongtouxu.service.Impl;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import per.kongtouxu.dao.AdminDao;
import per.kongtouxu.domain.pojo.Admin;
import per.kongtouxu.domain.pojo.AdminLog;
import per.kongtouxu.domain.pojo.PageBean;
import per.kongtouxu.domain.vo.V_Admin;
import per.kongtouxu.service.AdminService;

import java.util.List;
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
  private AdminLogServiceImpl adminLogService ;
  @Autowired
  private AdminDao adminDao;
    @Override
    public Admin login(Admin admin) {
        Admin loginAdmin = null;
            loginAdmin = adminDao.findAdminByUP(admin.getAdminName(), admin.getAdminPwd());
        return loginAdmin;
    }

    @Override
    public PageBean<V_Admin> findAdminByPage(String adminName, Integer roleId, String startTime, String endTime, int currentPage, int pageSize) {
        PageBean<V_Admin> pageBean=null;
        try {
            //1.分页类
            pageBean = new PageBean<>();
            //2.设置当前页码
            pageBean.setCurrentPage(currentPage);
            //3.设置每页显示条数
            pageBean.setPageSize(pageSize);
            //4.设置总记录条数
            int totalCount = adminDao.findCountAdminByPage(adminName,roleId,startTime,endTime);
            pageBean.setTotalCount(totalCount);
            //5.设置PageBean中的实体数据
            //开始的记录条数=（当前页码-1）*每页显示条数
            int start = (currentPage -1)*pageSize;
            List<V_Admin> lsit = adminDao.findAdminByPage(adminName,roleId,startTime,endTime, new RowBounds(start, pageSize));
            pageBean.setList(lsit);
            //6.设置总页数   总页数= 总记录条数/每页显示条数=0 ？ 总记录条数/每页显示条数=0 ：（总记录条数/每页显示条数=0）+1
            int totalPage = totalCount%pageSize==0 ? totalCount/pageSize : (totalCount/pageSize)+1;
            pageBean.setTotalPage(totalPage);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //7.返回
            return pageBean;
        }
    }

    @Override
    public Integer saveAdmin(Admin admin, AdminLog adminLog) {

        //2.执行
        Integer result = adminDao.saveAdmin(admin);
        System.out.println("result+"+result);
        if (result>0){
            adminLogService.saveAdminLog(adminLog);
        }
        //3.摧毁

        return result;
    }

    @Override
    public Integer updateAdmin(Admin admin, AdminLog adminLog) {
        //2.执行
        Integer result = adminDao.updateAdmin(admin);
        System.out.println("result+"+result);
        if (result>0){
            adminLogService.saveAdminLog(adminLog);
        }
        //3.摧毁
        return result;
    }

    @Override
    public Admin findAdminByAdminId(Integer adminId) {
        //2.执行
        Admin admin = adminDao.findAdminByAdminId(adminId);
        return admin;
    }
}
